var newsIndex = 5;
var baseUrl = "http://c.m.163.com/nc/article/headline/";
var catogary = 'T1348647909107';
//var baseUrl = "http://c.m.163.com/nc/article/headline/T1348649079062/";//体育
//var catogary = 'T1348649079062';//体育
var requestDelayTime = 600;
var webview_detail = null; //详情页webview
var titleNView = { //详情页原生导航配置
	backgroundColor: '#f7f7f7', //导航栏背景色
	titleText: '', //导航栏标题
	titleColor: '#000000', //文字颜色
	type: 'transparent', //透明渐变样式
	autoBackButton: true, //自动绘制返回箭头
	splitLine: { //底部分割线
		color: '#cccccc'
	},
	buttons: [{ //分享按钮
		'float': 'right',
		fontSrc: '_www/fonts/mui-icons-extra.ttf',
		text: '\ue200',
		onclick: showShare
	}]
}

mui.init({
	pullRefresh: {
		container: "#refreshContainer", //下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
		down: {
			style: 'circle', //必选，下拉刷新样式，目前支持原生5+ ‘circle’ 样式
			color: '#DD524D', //可选，默认“#2BD009” 下拉刷新控件颜色
			height: 35, //可选,默认50px.下拉刷新控件的高度,
			range: '100px', //可选 默认100px,控件可下拉拖拽的范围
			offset: '0px', //可选 默认0px,下拉刷新控件的起始位置
			auto: true, //可选,默认false.首次加载自动上拉刷新一次
			callback: pullDown //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
		},
		up: {
			height: 25, //可选.默认50.触发上拉加载拖动距离
			auto: false, //可选,默认false.自动上拉加载一次
			contentrefresh: "正在加载...", //可选，正在加载状态时，上拉加载控件上显示的标题内容
			contentnomore: '没有更多数据了', //可选，请求完毕若没有更多数据时显示的提醒内容；
			callback: getListData //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
		}
	}
});
var catogaryPicker = new mui.PopPicker();
catogaryPicker.setData([{
	value: 'T1348647909107',
	text: '头条'
}, {
	value: 'T1348648037603',
	text: '社会'
}, {
	value: 'T1467284926140',
	text: '要闻'
}, {
	value: 'T1348649079062',
	text: '体育'
}, {
	value: 'T1348649145984',
	text: 'NBA'
},{
	value:'T1348649176279',
	text:'国际足球'
},{
	value:'T1348649503389',
	text:'中国足球'
},{
	value:'T1350383429665',
	text:'轻松一刻'
}]);
mui.plusReady(function() {
	dragHandler(0);
	webview_detail = mui.preload({
		url: 'newsDetail.html',
		id: 'news_detail',
		styles: {
			"render": "always",
			"popGesture": "hide",
			"bounce": "vertical",
			"bounceBackground": "#efeff4",
			"titleNView": titleNView
		}
	});
});
document.addEventListener('changeCatogary', function(e) {
	changeCatogary();
});

function changeCatogary() {
	catogaryPicker.show(function(items) {
		mui.fire(plus.webview.getLaunchWebview(),'changeTitle',{title:items[0].text});
		plus.nativeUI.showWaiting();
		newsIndex = 5;
		h('#news').html('');
		catogary = items[0].value;
		initSlider();
		getListData();
	});
}

function initData() {
	initSlider();
	getListData();
}

function initSlider() {
	//获取新闻的图片轮播
	mui.get(baseUrl + catogary + '/0-10.html', function(res) {
		var data = res[catogary];
		var html = '';
		var indicator = '';
		for(var i = 0; i < 5; i++) {
			html += '<div class="mui-slider-item" data-docid="' + data[i].docid + '" data-time="' + data[i].ptime + '" title="' + data[i].title + '" data-cover="' + data[i].imgsrc + '"><a href="#">' +
				'<img src="' + data[i].imgsrc + '" alt="' + data[i].title + '"></a>' +
				'<div class="imageTitle">' + data[i].title + '</div></div>';
			if(i == 0) {
				indicator += '<div class="mui-indicator mui-active"></div> ';
			} else {
				indicator += '<div class="mui-indicator"></div>';
			}
		}
		h('.mui-slider-group').html(html);
		h('.mui-slider-indicator').html(indicator);
		//定义图片轮播
		var gallery = mui('.mui-slider');
		gallery.slider({});
		mui(".mui-slider-group").on('tap', '.mui-slider-item', function() {
			open_detail(this);
		});
		plus.nativeUI.closeWaiting();
	}, 'json');
}

function getListData() {
	var reqUrl = baseUrl + catogary + '/' + newsIndex + '-10.html';
	mui.get(reqUrl, function(res) {
		var data = res[catogary];
		var html = '';
		for(var i = 0; i < data.length; i++) {
			html += '<div class="list-item" data-digest="' + data[i].digest + '" data-docid="' + data[i].docid + '" data-time="' + data[i].ptime + '" title="' + data[i].title.replace(new RegExp('"', "gm"), '&quot;') + '" data-cover="' + data[i].imgsrc + '">' +
				'<div class="list-item-img"><img src="' + data[i].imgsrc + '"></div>' +
				'<div style="margin-left: 10px;">' +
				'<div class="list-item-title">' + data[i].title.replace(new RegExp('"', "gm"), "&quot;") + '</div>' +
				'<div class="list-item-desc">' + data[i].digest + '</div></div></div>';
		}

		h('#news').html(h('#news').html() + html);
		newsIndex = newsIndex + 10;
		mui('#refreshContainer').pullRefresh().endPulldown();
		mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
		mui("#news").on('tap', '.list-item', function() {
			open_detail(this);
		});
	});

}

function pullDown() {
	initData();
}

/**
 * 打开新闻详情
 * 
 * @param {Object} item 当前点击的新闻对象
 */
function open_detail(item) {
	//触发子窗口变更新闻详情
	mui.fire(webview_detail, 'get_detail', {
		title: item.title,
		time: item.dataset.time,
		cover: item.dataset.cover,
		digest: item.dataset.digest,
		docid: item.dataset.docid
	});

	//更改详情页原生导航条信息
	titleNView.titleText = item.title;
	webview_detail.setStyle({
		"titleNView": titleNView
	});
	setTimeout(function() {
		webview_detail.show("slide-in-right", 300);
	}, 150);
}

function showShare() {
	mui.fire(webview_detail, 'showShare');
}